package com.lonely.fx.dialog;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.Optional;
import java.util.function.Consumer;

/**
 * @date: 2022-08-01 17:30
 * @author: ztkj-hzb
 * @description: 待下拉框的对话框属性设置
 **/
public class ChoiceDialogSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        Button button = new Button("点击打开弹框");
        hBox.getChildren().addAll(button);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {


                ChoiceDialog<String> choiceDialog = new ChoiceDialog<>();

                //添加下拉列表数据
                choiceDialog.getItems().addAll("A","B","C","D");

                //设置下拉框选择的项目
                choiceDialog.setSelectedItem("B");

                choiceDialog.selectedItemProperty().addListener(new ChangeListener<String>() {
                    @Override
                    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                        System.out.println("当前选择：" + newValue);
                    }
                });

                Optional<String> optional = choiceDialog.showAndWait();

                optional.ifPresent(new Consumer<String>() {
                    @Override
                    public void accept(String s) {
                        System.out.println("选择的项目是：" + s);
                    }
                });

            }
        });


    }
}
